package com.pignest.hotel.ui.selfhelp;

import java.util.Date;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Label;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Card;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Memberchargehistrory;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbCloseAccounts;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.definition.ChargeOpType;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.PayMethod;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Global;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.system.Logic;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class DaySelfCheck extends BaseSelfCheckOutVbox {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2731869419525541714L;
	
	private Label used;
	private Label balance;
	private Label lAddDay;
	private Label lAddPrice;
	private Label lAllPrice;
	private ReceptionService receptionService;
	//private AuditService auditService;
	private MemberService memberService;
	public DaySelfCheck(){
		receptionService = Services.getReceptionService();
		//auditService = Services.getAuditServcie();
		memberService= Services.getMemberService();
		addEventListener("onCreate", this);
	}
	public void onEvent(Event event) throws Exception {
		super.onEvent(event);
		if ("onCreate".equals(event.getName()))
		{
			used=(Label)getFellow("used");
			balance=(Label)getFellow("balance");
			lAddDay=(Label)getFellow("lAddDay");
			lAddPrice=(Label)getFellow("lAddPrice");
			lAllPrice=(Label)getFellow("lAllPrice");
			dayInit();
		}
	}
	public void dayInit(){
		 Living l =super.getCurLiving();
		 Date liveTime =l.getLiveTime();
			String sCurTime =DateUtil.currentDateToString(DateUtil.DEFAULT_DATETIME_FORMAT);
			String sLiveTime =DateUtil.toString(liveTime, DateUtil.DEFAULT_DATETIME_FORMAT);
			int days =Logic.getLivingAdditionalTime(sLiveTime, sCurTime, false);
			int comsum =Services.getIncomeService().countAllConsumsUsed(l, true,null);//除加收外的消费
			int addPrice =0;
			float addays =days/2f;
			if(days!=0)
			{
				
				addPrice =l.getAdjustPrice()*days/2;//正数
			}
			lAddDay.setValue(String.valueOf(addays));
			lAddPrice.setValue(Utils.toPrice(addPrice));
			
		    used.setValue(Utils.toPrice(l.getUsed()));
		    balance.setValue(Utils.toPrice(l.getStorage()-comsum-addPrice));
		    lAllPrice.setValue(Utils.toPrice(comsum+addPrice));
	}
	public String selfCheckOut(Card card) {
		try {
			Living l =super.getCurLiving();
			boolean isCheckOut = receptionService.isCheckOut(l);
			if (isCheckOut)
				return "已结账";
			int deposit = Services.getIncomeService().countAllDeposit(l, false);
			if (deposit != 0) {
				return "抱歉你在前台有押金不能在自助上办理结账退房，请到前台办理!";
			}
			StringBuffer msg = new StringBuffer();
			Date liveTime = l.getLiveTime();
			String sCurTime = DateUtil
					.currentDateToString(DateUtil.DEFAULT_DATETIME_FORMAT);
			String sLiveTime = DateUtil.toString(liveTime,
					DateUtil.DEFAULT_DATETIME_FORMAT);
			int days = Logic
					.getLivingAdditionalTime(sLiveTime, sCurTime, false);
			// int storage=l.getStorage();//总存款
			int comsum = Services.getIncomeService().countAllConsumsUsed(l,
					true,null);// 除加收外的消费
			int price = l.getAdjustPrice();
			int addPrice = 0;
			int allPrice = 0;
			msg.append("你好:").append(l.getCustormName()).append("\n");
			msg.append("入住时间").append(sLiveTime).append("\n");
			msg.append("结账时间：").append(sCurTime).append("\n");
			TbAccounts ac = null;
			if (days != 0) {
				float fDays = days / 2f;
				String dayName = "";
				if (days == 1)
					dayName = "半天";
				else if (days == 2)
					dayName = "全天";
				else if (days == 3)
					dayName = "一天半";
				else if (days == 4)
					dayName = "两天";

				addPrice = price * days / 2;// 正数
				allPrice = comsum + addPrice;
				msg.append("当前总消费为:").append(Utils.toPrice(allPrice)).append("\n");
				msg.append("其中加收:").append(dayName).append("\n");
				msg.append("加收费用为：").append(Utils.toPrice(addPrice)).append("\n");
				ac = new TbAccounts();
				ac.setLiving(l);
				ac.setType(InComeItemType.JSFF.name());
				ac.setCounts(fDays);
				ac.setPrice(-addPrice);
				ac.setPrice2(price);
				ac.setName(dayName);
				ac.setEndstate("是");
				ac.setEndtime(new Date());
				ac.setOpdate(DateUtil.toString(new Date(), "yyyy-MM-dd"));
				ac.setOptime(new Date());
				ac.setOpUser(l.getMember().getName());
				ac.setRemark("加收" + dayName + "共计:" + Utils.toPrice(addPrice));

			} else {
				allPrice = comsum;
			}
			msg.append("当前总消费为:").append(Utils.toPrice(allPrice)).append("\n");
			if (UIUtils.confirm(msg.toString())) {
				// 当会员卡不够消费
				int balance = memberService.queryMemberBalance(l.getMember());
				int lost = balance - allPrice;
				if (lost <= 0) {
					UIUtils.alert("会员余额不足" + Utils.toPrice(lost) + "！请及时充值！谢谢");
				}
				// 设置离开时间
				l.setLeaveDate(new Date());
				l.setLiveState(LivingState.CHECKOUT.name());
				// 会员加收时增加记录
				Memberchargehistrory mch = null;
				if (addPrice != 0) {
					mch = new Memberchargehistrory();
					mch.setMember(l.getMember());
					mch.setChargeTime(new Date());
					mch.setPrice(-addPrice);
					mch.setChargetype("自助");
					mch.setOptype(ChargeOpType.OnConsume.name());
					mch.setRemark(DateUtil.curDateStr()
							+ ChargeOpType.OnCharge.getValue()
							+ Utils.toPrice(addPrice));
				}
				TbCloseAccounts ca = new TbCloseAccounts();
				ca.setLiving(l);
				ca.setInvoice(0);
				ca.setTime(new Date());
				ca.setAccounts(allPrice);
				ca.setType(PayMethod.MEMBER_CARD.name());
				ca.setOrderNo(DateUtil.toString(l.getLiveTime(), "yyyyMMddHHmmss"));
				ca.setCheckOutNo(l.getMember().getCardNo());
				ca.setSerialNumber(Global.getLivineSerialNumber());
				User user = Hotel.getCurrentUser();
				ca.setUsername(user.getName());
				ca.setUserId(user.getId());
				l.setTbColoseAccounts(ca);
				receptionService.updateSelfCheckOut(l, ac, ca, mch, card);
				return null;
			} else
				return "已取消";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
}
